<?php
if (!defined('BASEPATH')) exit('No direct access allowed.'); 

class MY_Controller extends CI_Controller 
{
	public $user_id;
    public function __construct() 
    { 
        parent::__construct();

        $this->load->database();
        $this->load->helper('url');
		$this->load->helper('Functions');
		
		
        $this->load->library('session');  
		
		
        $this->image_load_url = base_url().'/imgServer/index.php';

		$this->checkLogin();
    } 



	//检查登录
	public function checkLogin() 
	{
        //获取管理员id
        $user_id = $this->session->userdata('admin_id');
        $login =  $this->uri->segment(1);

        //如果SESSION不存在，则跳转到登录
        if ((!isset($user_id) || intval($user_id) <= 0) && $login = 'login')
        {
            //跳转
            redirect(base_url('admin.php/Login/login'));
			return false;
        }

        return TRUE;
    }
	
	public function message($msg,$url=''){

		$script = "<script>";
		$script .= "alert('{$msg}');";
		if(!empty($url)){
			$script .= "location.href='{$url}';";
		}
		else{
			$script .= "history.go(-1);";
		}
		$script .= "</script>";
		die($script);
	}
	

	public function out($data=array()) {
       $arr = array('result_code'=>0,'message'=>'','data'=>'');
	   
	   foreach($data as $key=>$val){
		   $arr[$key] = $val;
	   }
	   echo json_encode($arr);
	   exit;
    }
	
	public function data_out($data=array(),$message="获取数据成功"){
		
		$arr = array('result_code'=>0,'message'=>$message,'data'=>$data);
	    $this->out($arr);
	}
	public function err_out($message,$result_code=1) {
       $arr = array('result_code'=>$result_code,'message'=>$message);
	   $this->out($arr);
    }
	function CheckData($arr=array(),$data=array(),$is_empty=false){
		//验证参数
		$this->load->library('form_validation');
		
		$config = array();
		
		
		if($is_empty){
			foreach($data as $key=>$val){
				$data[$key] = !empty($val)?$val:'';
			}
		}
		
		foreach($arr as $key=>$val){
			if(is_array($val)){

				
				$ls_arr = array();
				$errors = array();
				
				foreach($val as $k=>$v){
					$ls_arr[] = $k;
					$ls_key = preg_replace("/\[.*/",'',$k);
					$errors[$ls_key] = $v;
				}
				$rules = implode('|',$ls_arr);
			}
			else{
				$rules = 'required';
				$errors = array(
					$rules =>$val
				);
			}
			
			$config[] = array(
					'field' => $key,
					'label' => $key,
					'rules' => $rules,
					'errors' => $errors,
				);
		}
		
		$this->form_validation->set_data($data);
		$this->form_validation->set_rules($config);

		if($this->form_validation->run() === FALSE){
			$err_arr = $this->form_validation->error_array();
			
			return implode(PHP_EOL,$err_arr);
        }
		return false;
	}
}


?>